package org.jshand.jxc.business;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.WindowConstants;

import org.jshand.jxc.business.book.panel.LeftMenu;
import org.jshand.jxc.business.book.panel.StatusPanel;
import org.jshand.jxc.business.book.panel.WelcomePanel;
import org.jshand.jxc.framework.AboutDialog;
import org.jshand.jxc.util.SwingUtils;

public class BusinessFrame extends JFrame implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = -5607975495058231163L;
	
	private JMenu fileMenu;
	private JMenuItem openItem;
	private JPanel menuPanel;
	private JPanel contentPanel;
	private Graphics g2d;

	private JPanel toolBarPanel;

	private JToolBar tb;

	private JMenu helpMenu;

	private JMenuItem helpItem;

	private StatusPanel statusBar;

	private JMenuItem metalLookAndFeelMenu;

	private JMenuItem windowsLookAndFeelMenu;

	private JMenuItem motifLookAndFeelMenu;

	private JMenuItem nimbusLookAndFeelMenu;
	
	
	
	

	public BusinessFrame() throws HeadlessException {
		super();
		setTitle("你好");
		setSize(980,600);
//		setLocation(200, 80);
		setLocationRelativeTo(null);
		setResizable(false);
		setIconImage(SwingUtils.getImage("application_form_magnify.png"));
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//		this.getContentPane().setBackground(Color.CYAN);
		//初始化菜单栏
		initMenubar();
		
		this.setLayout(new BorderLayout(1,1));

		SwingUtils.setMainFrame(this);
		
		initComponent();
		
	}

	private void initComponent(){
		//初始化工具栏
		initToolBar();
		
		//初始化布局Panel
		
		//初始化左侧容器
		initLeftMenu();
		
		//初始化右侧容器
		initContentPanel();
		
		initStatusBar();

		setVisible(true);
		
		setMsg("系统加载完毕......");
		
	}
	
	private void initStatusBar() {
		statusBar = new StatusPanel();
		statusBar.setPreferredSize(new Dimension(1, 26));
		this.add(statusBar,BorderLayout.SOUTH);
	}

	private void initToolBar() {

		toolBarPanel = new JPanel();
		toolBarPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		toolBarPanel.setBackground(Color.CYAN);
		tb = new JToolBar();
		tb.add(new JButton(" 测试  "));
		tb.add(new JButton(SwingUtils.getImageIcon("arrow_down.png")));
		tb.add(new JButton(SwingUtils.getImageIcon("arrow_left.png")));
		tb.add(new JButton(SwingUtils.getImageIcon("arrow_switch.png")));
		tb.add(new JButton(SwingUtils.getImageIcon("arrow_switch.png")));
		toolBarPanel.add(tb);
		this.add(toolBarPanel,BorderLayout.NORTH);
	}

	private void initContentPanel() {
		//工作台面板
		contentPanel = new WelcomePanel();
		contentPanel.setPreferredSize(new Dimension(120, 1));
		contentPanel.setSize(100,300);
		SwingUtils.addContentList(contentPanel);
		this.add(contentPanel,BorderLayout.CENTER);
		
	}

	private void initLeftMenu() {

		//菜单Panel
		menuPanel = new LeftMenu(this);
		menuPanel.setPreferredSize(new Dimension(120, 1));
		this.add(menuPanel,BorderLayout.WEST);
		
	}


	/**
	 * 添加菜单栏
	 */
	private void initMenubar() {
		JMenuBar mb = new JMenuBar();
		fileMenu = new JMenu("文件");
		openItem = new JMenuItem("打开");
		fileMenu.add(openItem);
		
		helpMenu = new JMenu("帮助");
		helpItem = new JMenuItem("关于我们");
		helpMenu.add(helpItem);
		helpItem.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				AboutDialog dialog = new AboutDialog(BusinessFrame.this, true);
				dialog.setLocationRelativeTo(null);
				dialog.setIconImage(SwingUtils.getImage("cake_sliced.png"));
				dialog.setVisible(true);
			}
		});

		
		mb.add(fileMenu);
		mb.add(helpMenu);
		
		JMenu lookAndFellMenu = new JMenu("外观(LookAndFell)");
		
		metalLookAndFeelMenu = new JMenuItem("Metal风格");
		metalLookAndFeelMenu.addActionListener(this);
		lookAndFellMenu.add(metalLookAndFeelMenu);
		
		windowsLookAndFeelMenu = new JMenuItem("Motif风格");
		windowsLookAndFeelMenu.addActionListener(this);
		lookAndFellMenu.add(windowsLookAndFeelMenu);
		
		motifLookAndFeelMenu = new JMenuItem("Motif风格");
		motifLookAndFeelMenu.addActionListener(this);
		lookAndFellMenu.add(motifLookAndFeelMenu);
		
		nimbusLookAndFeelMenu = new JMenuItem("Nimbus风格");
		nimbusLookAndFeelMenu.addActionListener(this);
		lookAndFellMenu.add(nimbusLookAndFeelMenu);
		
		mb.add(lookAndFellMenu);
		
		this.setJMenuBar(mb);
	}

	

	
	
	public void setMsg(String msg){
		statusBar.setMessage(msg);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		this.getContentPane().removeAll();
		
		
		
		//外观选择
		if(source == metalLookAndFeelMenu){
			setUI(1);
		}else if(source == windowsLookAndFeelMenu){
			setUI(2);
		}else if(source == motifLookAndFeelMenu){
			setUI(3);
		}else if(source == nimbusLookAndFeelMenu){
			setUI(4);
		}
		
	}
	
	public void setUI(int type){
		try {
			switch (type) {
				case 1:
					//1、（官方）MetalLookAndFeel（Swing 默认的 LookAndFeel）：
					UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
					break;
				case 2:
					//2、（官方）WindowsLookAndFeel：
					UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
					break;
				case 3:
					//3、（官方）MotifLookAndFeel：
					UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
					break;
				case 4:
					//4、（官方）NimbusLookAndFeel：
					UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
					break;
				default:
					break;
			}

			SwingUtilities.updateComponentTreeUI(this);
			
			initComponent();
			
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		} catch (InstantiationException e1) {
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			e1.printStackTrace();
		} catch (UnsupportedLookAndFeelException e1) {
			e1.printStackTrace();
		}
	}
	
	public static void main(String[] args) throws InterruptedException {
		new BusinessFrame();
		
	}
	
	
	
}
